jQuery(document).ready(function($){
  var err='';
  var myPicker = new jscolor.color(document.getElementById('widget_colour'), {hash:true,pickerFaceColor:'#87CEEB'});
  myPicker.fromString('000000');
  
  function IsNumeric(strString)
 //  check for valid numeric strings	
 {
 var strValidChars = "0123456789.-";
 var strChar;
 var blnResult = true;

 if (strString.length == 0) return false;

 //  test strString consists of valid characters listed above
 for (i = 0; i < strString.length && blnResult == true; i++)
    {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
       {
       blnResult = false;
       }
    }
 return blnResult;
 }
  
  $("#widget_colour").change(function(){
      $('#contentDiv').css({'background': '#'+myPicker});
      $('.item').css({'border-bottom': '1px groove #'+myPicker});
  });
  
  if((IsNumeric($("#width").val())) != 0)
  {
    $("#width").keyup(function(){
        $('#contentDiv').css({'width': $("#width").val()+'px'});
    });
  }
  
  $("#submit").click(function(){       
       if($("#users_name").val().length === 0)
       {
        err += 'Please Enter a Your Name\n';
        var error = 1;
       }
       
      function isEmail(Mail) {
        Mail=Mail.toLowerCase();
        return (Mail.search(/^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/) != -1);
      }

      function urlCheck(str) {
        var v = new RegExp();
        v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
        if (!v.test(str)) {
        return false;
        }
        return true;
      }


       if($("#email").val().length === 0)
       {
        err += 'Please Enter a Email Address\n';
        var error = 1;
       }
       else
       {
        if((isEmail($("#email").val())) == 0)
        {
          err += 'Email Address Entered is Invalid\n';
          var error = 1;
        }
       }
       
       if($("#width").val().length === 0)
       {
        err += 'Please Enter a Width in Pixels\n';
        var error = 1;
       }
       else
       {
        if((IsNumeric($("#width").val())) == 0)
        {
          err += 'Width can be only Numeric\n';
          var error = 1;
        }
       }
       
       if($("#website").val().length === 0)
       {
        err += 'Please Enter Your Website\n';
        var error = 1;
       }
       else
       {
        if((urlCheck($("#website").val())) == 0)
        {
          err += 'Please Enter a Valid Website Address\n';
          var error = 1;
        }
       }
       
       if(error == 1)
       {
          alert(err);
          err = '';
       }
       else
       {
          var users_name = $('#users_name').val();
          var user_id = $('#user_id').val();
          var user_slug = $('#user_slug').val();
          var email = $('#email').val();
          var website = $('#website').val();
          var width = $('#width').val();
          var widget_color = '#'+myPicker;
          var type = $('input:radio:checked').val();
          var city = $('#city').val();
        
          $.ajax({ 
          	type: "post", 
          	url: "http://buzzr.in/wp-content/plugins/buzzr_api/api_key.php", data: "user_id="+encodeURIComponent(user_id)+"&user_slug="+encodeURIComponent(user_slug)+"&users_name="+encodeURIComponent(users_name)+"&email="+encodeURIComponent(email)+"&website="+encodeURIComponent(website)+"&width="+encodeURIComponent(width)+"&widget_color="+encodeURIComponent(widget_color)+"&type="+encodeURIComponent(type)+"&city="+encodeURIComponent(city),
          	success: function(msg){
          		$(".result1").empty();
          		$("#users_name").val('');
                $("#email").val(''); 
                $("#website").val('');
                $("#width").val('250');
                myPicker.fromString('000000');
                $("#city").val('mumbai');
                $("input:radio").val('');
          		$(".result1").append(msg);
          	   }
          	});
       }
    });
    
    $('.loading').hide().ajaxStart(function() {
            $('.result1').hide();            
            $(this).show();
    }).ajaxStop(function() {
            $(this).hide();
            $('.result1').show();
    });
    
});